package com.zc.angelica.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zc.angelica.entity.ButtonInfo;
import com.zc.angelica.entity.ResponseData;
import com.zc.angelica.mapper.ButtonMapper;
import com.zc.angelica.service.ButtonInfoService;
import org.springframework.stereotype.Service;

/**
 * 按钮信息表(Button)表服务实现类
 *
 * @author makejava
 * @since 2022-04-08 22:03:31
 */
@Service
public class ButtonInfoServiceImpl extends ServiceImpl<ButtonMapper, ButtonInfo> implements ButtonInfoService {

    @Override
    public ResponseData addButton(ButtonInfo buttonInfo) {
        if (buttonInfo == null) {
            return ResponseData.error("请求参数不能为空");
        }
        if (buttonInfo.getButtonCode() == null || buttonInfo.getButtonCode().equals("")) {
            return ResponseData.error("菜单code不能为空");
        }
        QueryWrapper<ButtonInfo> buttonWrapper = new QueryWrapper<>();
        buttonWrapper.eq("button_code", buttonInfo.getButtonCode());
        long codeCount = this.count(buttonWrapper);
        if (codeCount > 0) {
            return ResponseData.error("菜单code已经存在");
        }
        boolean save = this.save(buttonInfo);
        if (save) {
            return ResponseData.success("添加按钮成功");
        }
        return ResponseData.error("添加按钮失败");
    }

    @Override
    public ResponseData deleteButton(Long id) {
        boolean delete = this.removeById(id);
        if (delete) {
            return ResponseData.success("删除按钮成功");
        }
        return ResponseData.error("删除按钮失败");
    }

    @Override
    public ResponseData updateButton(ButtonInfo buttonInfo) {
        if (buttonInfo.getId() != null) {
            boolean update = this.updateById(buttonInfo);
            if (update) {
                return ResponseData.success("更新按钮成功");
            }
            return ResponseData.error("更新按钮失败");
        }
        return ResponseData.error("请求参数不能为空");
    }
}

